package com.vhly.remoteapi.yahoo.contact;

import com.vhly.remoteapi.yahoo.contact.field.*;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-5
 * Time: 15:55:20
 * Email: vhly@163.com
 */
public class Field extends ContactObject {
    public String title;
    public String nickname;
    public boolean isConnection;
    public Flags flags;
    public FieldType type;
    public FieldValue value; // String or Field

    /**
     * Read object from ObjectInput
     *
     * @param din ObjectInput
     */
    @Override
    protected void readObject(DataInput din) throws IOException {
        super.readObject(din);
        isConnection = din.readBoolean();
        title = din.readUTF();
        nickname = din.readUTF();
        String s = din.readUTF();
        // TODO parse s to flags
        s = din.readUTF();
        short tp = din.readShort();
        value = FieldValueProcess.makeFieldValue(tp);
        if(value != null){
            value.readObject(din);
        }
    }

    /**
     * Write object to ObjectOutput
     *
     * @param dout Output
     */
    @Override
    protected void writeObject(DataOutput dout) throws IOException {
        super.writeObject(dout);
        dout.writeBoolean(isConnection);
        dout.writeUTF(title);
        dout.writeUTF(nickname);
        dout.writeUTF(flags.toString());
        dout.writeUTF(type.toString());
        int tp = FieldValueProcess.getFieldValueType(value);
        dout.writeShort(tp);
        if(tp != -1){
            value.writeObject(dout);
        }
    }

    @Override
    public void parse(Element item) {
        super.parse(item);

        NodeList lst = item.getElementsByTagName("type");
        if (lst != null) {
            int len = lst.getLength();
            if (len > 0) {
                Element el = (Element) lst.item(0);
                NodeList cs = el.getChildNodes();
                if (cs != null && cs.getLength() > 0) {
                    Node nd = cs.item(0);
                    if (nd instanceof Text) {
                        Text txt = (Text) nd;
                        String t = txt.getNodeValue();
                        NodeList vls = item.getElementsByTagName("value");
                        if (vls != null && vls.getLength() > 0) {
                            // guid, nickname, email, yahooid, otherid, phone, jobtitle, company, notes, link, custom, name, address, birthday, anniversary
                            if (t.equals("guid")) {
                                type = FieldType.guid;
                                value = new TextField("");
                            } else if (t.equals("nickname")) {
                                type = FieldType.nickname;
                                value = new TextField(null);
                            } else if (t.equals("name")) {
                                type = FieldType.name;
                                value = new Name(null);
                            } else if (t.equals("phone")) {
                                type = FieldType.phone;
                                value = new TextField("");
                            } else if (t.equals("email")) {
                                type = FieldType.email;
                                value = new TextField("");
                            } else if (t.equals("address")) {
                                type = FieldType.address;
                                value = new Address();
                            } else if (t.equals("birthday")) {
                                type = FieldType.birthday;
                                value = new Birthday();
                            } else if (t.equals("yahooid")) {
                                type = FieldType.yahooid;
                                value = new YahooID();
                            } else if (t.equals("otherid")) {
                                type = FieldType.otherid;
                                value = new OtherID();
                            } else if (t.equals("jobtitle")) {
                                type = FieldType.jobtitle;
                                value = new TextField("");
                            } else if (t.equals("company")) {
                                type = FieldType.company;
                                value = new Company();
                            } else if (t.equals("notes")) {
                                type = FieldType.notes;
                                value = new Notes();
                            } else if (t.equals("link")) {
                                type = FieldType.link;
                                value = new TextField("");
                            } else if (t.equals("anniversary")) {
                                type = FieldType.anniversary;
                                value = new Anniversary();
                            }
                            Element ei = (Element) vls.item(0);
                            value.parse(ei);
                        }
                    }
                }
            }
        }
    }
}
